#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN WEB SERVER

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For use in commercial software, please obtain a commercial license from THN Solutions LLC.    

  + For use in free software, this library is licensed under GPL version 3. A summary of 
	GPLv3 is listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt 
	included with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 16-Nov-12 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.IO;

using Thn;
#endregion

namespace Thn.Web
{
    /// <summary>
    /// This resource read the file directly from disk on each read operation.
    /// </summary>
#if DEBUG || TESTING
    public
#else
    internal
#endif
 class DirectFileResource : FileResourceBase
    {
        #region Fields
        #endregion

        #region Properties
        #endregion

        #region On Read
        /// <summary>
        /// Attempt to read a number of bytes from the desired file into the target buffer.
        /// </summary>        
        /// <param name="buffer">The buffer to contain the data read.</param>
        /// <param name="fileOffset">Zero-based location from the beginning of file.</param>
        /// <param name="bytesToRead">Number of bytes to be read.</param>
        /// <param name="actualBytesRead">Number of bytes actually read.</param>
        /// <returns>Status of this operation.</returns>
        protected override FileReadStatus OnRead(byte[] buffer, long fileOffset, int bytesToRead, out int actualBytesRead)
        {
            FileReadStatus result;
            FileStream stream = null;

            lock (this)
            {
                try
                {
                    string filePath = Uri;
                    stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    stream.Seek(fileOffset, SeekOrigin.Begin);
                    actualBytesRead = stream.Read(buffer, 0, bytesToRead);
                    result = FileReadStatus.Success;
                }
                catch (Exception ex)
                {
                    if (ex is FileNotFoundException) result = FileReadStatus.NotFound;
                    else if (ex is UnauthorizedAccessException) result = FileReadStatus.NotAllowed;
                    else result = FileReadStatus.Other;
                    actualBytesRead = 0;
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }
            }

            return result;
        }
        #endregion        

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public DirectFileResource()
        { }
        #endregion
    }
}
